﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using CoreEx.Common.Linq;
using LinFu.IoC.Configuration;

namespace CoreEx.Database.Linq.Implementation
{
    [Implements(typeof(IExpressionRewriter), ServiceName = "StringRewriter")]
    public class StringRewriter : ExpressionVisitor, IExpressionRewriter
    {
        public Expression Rewrite(Expression expression)
        {
            return Visit(expression);
        }

        protected override Expression VisitMemberAccess(MemberExpression m)
        {
            if (m.Member.DeclaringType == typeof(string) && m.Member.Name == "Length")
            {
                var getMethod = typeof (string).GetProperty("Length").GetGetMethod();
                var test = Expression.Convert(Expression.Call(m.Expression, getMethod),m.Type);
                return test;
            }
            return m;
        }
    }
}
